package com.sip.stream.activity.preview.model;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

import com.sip.stream.media.ZFYMediaManager;

import java.lang.ref.WeakReference;

public class SensorViewModel {

    private static final String TAG = "SensorViewModel";

    public static final int KNOCK_PROTECT = 1;
    public static final int VIDEO_PROTECT = 2;

    private Float ACCELEROMETER_VALUE;
    private SensorEventListener accelerRemoterSensorEventListener;
    private WeakReference<Context> contextWeakReference;
    private SensorEventListener knockSensorEventListener;
    long lastKnockVideoProtectTime;
    private long lastVideoProtectTime;
    private SensorViewModelListener listener;

    public interface SensorViewModelListener {
        void onSensorEvent(int i);
    }

    public void onAudioStart() {}

    public void onAudioStop() {}

    public void onPause() {}

    private SensorViewModel() {
        this.ACCELEROMETER_VALUE = Float.valueOf(6.0f);
        this.lastVideoProtectTime = 0L;
        this.lastKnockVideoProtectTime = 0L;
    }

    public void setListener(SensorViewModelListener sensorViewModelListener) {
        this.listener = sensorViewModelListener;
    }

    public SensorViewModel(Context context) {
        this.ACCELEROMETER_VALUE = Float.valueOf(6.0f);
        this.lastVideoProtectTime = 0L;
        this.lastKnockVideoProtectTime = 0L;
        this.contextWeakReference = new WeakReference<>(context);
    }

    private Context getContext() {
        return this.contextWeakReference.get();
    }

    public void onResume() {
        Log.i(TAG, "onResume");
        if (ZFYMediaManager.getInstance().getStatus().isVideoStart()) {
            return;
        }
        openKnockProtect();
    }

    public void release() {
        Log.i(TAG, "release closeKnockProtect");
        closeKnockProtect();
    }

    public void onVideoStart() {
        Log.d(TAG, "onVideoStart");
        openVideoProtect();
        closeKnockProtect();
    }

    public void onVideoStop() {
        Log.d(TAG, "onVideoStop");
        closeVideoProtect();
        openKnockProtect();
    }

    private void openVideoProtect() {
        if (this.accelerRemoterSensorEventListener == null && ZFYMediaManager.getInstance().getConfig().isVideoProtect()) {
            this.lastVideoProtectTime = System.currentTimeMillis();
            SensorManager sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
            sensorManager.registerListener(getAccelerRemoterSensorEventListener(), sensorManager.getDefaultSensor(1), 3);
        }
    }

    private void closeVideoProtect() {
        if (this.accelerRemoterSensorEventListener != null) {
            ((SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE)).unregisterListener(this.accelerRemoterSensorEventListener);
            this.accelerRemoterSensorEventListener = null;
        }
    }

    private SensorEventListener getAccelerRemoterSensorEventListener() {
        if (this.accelerRemoterSensorEventListener == null) {
            this.lastVideoProtectTime = 0L;
            this.accelerRemoterSensorEventListener = new SensorEventListener() {
                @Override
                public void onAccuracyChanged(Sensor sensor, int i) {
                }

                @Override
                public void onSensorChanged(SensorEvent sensorEvent) {
                    SensorViewModel.this.videoProtect(sensorEvent);
                }
            };
        }
        return this.accelerRemoterSensorEventListener;
    }

    public synchronized void videoProtect(SensorEvent sensorEvent) {
        long currentTimeMillis = System.currentTimeMillis();
        long j = this.lastVideoProtectTime;
        if (currentTimeMillis - j < 0) {
            this.lastVideoProtectTime = currentTimeMillis;
        } else if (currentTimeMillis - j < 10000) {
        } else {
            if (sensorEvent.sensor.getType() == 1) {
                float f = sensorEvent.values[0];
                float f2 = sensorEvent.values[1];
                float f3 = sensorEvent.values[2];
                if (Math.sqrt((f * f) + (f2 * f2) + (f3 * f3)) < this.ACCELEROMETER_VALUE.floatValue()) {
                    this.lastVideoProtectTime = currentTimeMillis;
                    onSensorEvent(2);
                }
            }
        }
    }

    private void openKnockProtect() {
        if (ZFYMediaManager.getInstance().getConfig().isKnockProtect()) {
            Log.i(TAG, "openKnockProtect registerListener");
            try {
                SensorManager sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
                sensorManager.registerListener(getKnockSensorEventListener(), sensorManager.getDefaultSensor(1), 3);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.i(TAG, "registerListener end");
        }
    }

    private void closeKnockProtect() {
        if (this.knockSensorEventListener != null) {
            ((SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE)).unregisterListener(this.knockSensorEventListener);
            this.knockSensorEventListener = null;
        }
    }

    private SensorEventListener getKnockSensorEventListener() {
        if (this.knockSensorEventListener == null) {
            this.lastKnockVideoProtectTime = 0L;
            this.knockSensorEventListener = new SensorEventListener() {
                @Override
                public void onAccuracyChanged(Sensor sensor, int i) {
                }

                @Override
                public void onSensorChanged(SensorEvent sensorEvent) {
                    SensorViewModel.this.knockProtect(sensorEvent);
                }
            };
        }
        return this.knockSensorEventListener;
    }

    protected synchronized void knockProtect(SensorEvent sensorEvent) {
        if (sensorEvent.sensor.getType() == 1) {
            float f = sensorEvent.values[0];
            float f2 = sensorEvent.values[1];
            float f3 = sensorEvent.values[2];
            if (Math.sqrt((f * f) + (f2 * f2) + (f3 * f3)) < this.ACCELEROMETER_VALUE.floatValue()) {
                closeKnockProtect();
                onSensorEvent(1);
            }
        }
    }

    private void onSensorEvent(int i) {
        Log.i(TAG, "onSensorEvent " + i);
        SensorViewModelListener sensorViewModelListener = this.listener;
        if (sensorViewModelListener != null) {
            sensorViewModelListener.onSensorEvent(i);
        }
    }
}
